package com.app.service.Impl;

import com.app.mapper.CategoryMapper;
import com.app.model.CategoryModel;
import com.app.service.CategoryService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Page<CategoryModel> categoryPageList(int page, int limit, String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "name", "sort", "status").like("name", name).orderByAsc("id").orderByDesc("sort");
        Page<CategoryModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<CategoryModel> categoryList = categoryMapper.selectPage(pages, queryWrapper);
        return categoryList;
    }

    @Override
    public List<CategoryModel> selectPerTree(String keyWord) {//全查配置
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (StringTool.isNotEmpty(keyWord)) {
            queryWrapper.like("name", keyWord);
        }
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public void insert(CategoryModel model) {
        categoryMapper.insert(model);
    }

    @Override
    public void update(CategoryModel model) {
        categoryMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public CategoryModel getCategoryByName(String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "name", "sort", "status").eq("name", name);
        return categoryMapper.selectOne(queryWrapper);
    }
    @Override
    public List<CategoryModel> getCategoryByPid(Integer pid) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name").eq("pid",pid);
        List<CategoryModel> modelList =  categoryMapper.selectList(queryWrapper);
        return modelList;
    }
    @Override
    public CategoryModel getCategoryById(int id) {
        return categoryMapper.selectById(id);
    }

}
